import { http } from '@kit.NetworkKit'

class HttpUtils {
  httpRequest: http.HttpRequest = http.createHttp()

  async get<T>(url: string, data: string): Promise<T> {
    let res = await this.httpRequest.request(url, {
      expectDataType: http.HttpDataType.STRING,
      method: http.RequestMethod.GET,
      extraData: data
    })
    return JSON.parse(res.result as string) as T
  }

  async post<T>(url: string, data: string): Promise<T> {
    let res = await this.httpRequest.request(url, {
      expectDataType: http.HttpDataType.STRING,
      method: http.RequestMethod.POST,
      extraData: data
    })
    return JSON.parse(res.result as string) as T
  }

  async delete<T>(url: string, data: string): Promise<T> {
    let res = await this.httpRequest.request(url, {
      expectDataType: http.HttpDataType.STRING,
      method: http.RequestMethod.DELETE,
      extraData: data
    })
    return JSON.parse(res.result as string) as T
  }
}

export const httpUtils = new HttpUtils()